// store.js
import { searchApi } from '@/api/search'
import {getImageColor} from "../../utils/common";

let playerConfig = window.storage.get('playerConfig') || {}
let currentMusicInfo = window.storage.get('currentMusic') || {}

const store = {
  // 命名空间
  namespaced: true,
  state: {
    // 音乐信息
    music: currentMusicInfo.musicInfo || {},
    // 音乐详情信息
    musicDetails: currentMusicInfo.musicDetails || {},
    // 是否播放
    isPlay: false,
    // 是否播放完毕
    isPlayed: false,
    // 是否加载中
    loading: false,
    // 开始时间
    startTime: 0,
    // 结束时间
    endTime: 0,
    // 音量大小 0-1
    volume: playerConfig.volume || 0.5,
    // 进度条-进度 0-1
    progressSchedule: 0,
    // 歌词信息
    lyricInfo: playerConfig.lyricInfo || {},
    // 歌词显示
    lyricShow: true,
    musicDrawerShow: false,
    completeMusicDrawerShow: false,
    // 自动播放
    autoPlay: false,
    musicLyricPreview: false,
    // 最新歌词
    currentLyric: '',
    themeColor: playerConfig.playerThemeColor || '',
    likeMusicList: playerConfig.likeMusicList || [],
    isLikeMusic: playerConfig.isLikeMusic || false,
  },
  mutations: {
    setIsLikeMusic(state, isLikeMusic) {
      state.isLikeMusic = isLikeMusic
    },
    setLikeMusicList(state, likeMusicList) {
      state.likeMusicList = likeMusicList
    },
    setThemeColor(state, themeColor) {
      state.themeColor = themeColor
    },
    setMusicLyricPreview(state, musicLyricPreview) {
      state.musicLyricPreview = musicLyricPreview
    },
    setCurrentLyric(state, currentLyric) {
      state.currentLyric = currentLyric
    },
    openMusicDrawerShow(state) {
      state.musicDrawerShow = true
    },
    closeMusicDrawerShow(state) {
      state.musicDrawerShow = false
    },
    openCompleteMusicDrawerShow(state) {
      state.completeMusicDrawerShow = true
    },
    closeCompleteMusicDrawerShow(state) {
      state.completeMusicDrawerShow = false
    },
    /** 下一首音乐 */
    nextMusic(state, music) {
      state.music = music
      state.isPlay = true
    },
    /** 上一首音乐 */
    forwardMusic(state, music) {
      state.music = music
      state.isPlay = true

    },
    playMusic(state, music) {
      state.music = music
    },
    saveMusicDetails(state, musicDetails) {
      state.musicDetails = musicDetails
    },
    stopMusic(state) {
      state.isPlay = false
    },
    continuePlayMusic(state) {
      state.isPlay = true
    },
    /** 音量改变 */
    changeVolume(state, volume) {
      state.volume = volume
    },
    /** 进度条-进度改变 */
    changeProgressSchedule(state, progressSchedule) {
      state.progressSchedule = progressSchedule
    },
    /** 保存歌词信息 */
    saveLyricInfo(state, lyricInfo) {
      state.lyricInfo = lyricInfo
    },
    /** 设置歌词是否显示 */
    setLyricShow(state, lyricShow) {
      state.lyricShow = lyricShow
    },
    /** 开启自动播放 */
    startAutoPlay(state) {
      state.autoPlay = true
    }
  },
  actions: {
    setCurrentLyric({ commit }, currentLyric) {
      commit('setCurrentLyric', currentLyric)
    },
    /** 音量改变 */
    changeVolume({ commit }, volume) {
      playerConfig.volume = volume
      window.storage.set('playerConfig', playerConfig)
      commit('changeVolume', volume)
    },
    /** 进度条改变 */
    changeProgressSchedule({ commit }, progressSchedule) {
      playerConfig.progressSchedule = progressSchedule
      window.storage.set('playerConfig', playerConfig)
      commit('changeProgressSchedule', progressSchedule)
    },
    /** 歌词预览 */
    setMusicLyricPreview({ commit }, musicLyricPreview) {
      commit('setMusicLyricPreview', musicLyricPreview)
    },
    /** 下一首音乐 */
    nextMusic({ commit }, music) {
      commit('nextMusic', music)
    },
    /** 上一首音乐 */
    forwardMusic({ commit }, music) {
      commit('forwardMusic', music)
    },
    /** 播放音乐根据MusicId */
    async playMusic({ commit }, [music, autoPlay]) {
      // 需要获取音乐详情
      const res = await searchApi().getMusicUrlById({
        id: music.id
      })
      const lyricRes = await searchApi().getMusicLyricById({
        id: music.id
      })
      playerConfig.lyricInfo = lyricRes
      window.storage.set('playerConfig', playerConfig)
      // res.data => Array
      const musicDetails = res.data.find(v => v)
      window.storage.set('currentMusic', {
        musicInfo: music,
        musicDetails: musicDetails
      })
      if (!playerConfig.likeMusicList) {
        playerConfig.likeMusicList = []
      }
      const isLikeMusic = music.id in playerConfig.likeMusicList
      playerConfig.isLikeMusic = isLikeMusic
      window.storage.set('playerConfig', playerConfig)
      commit('setIsLikeMusic', isLikeMusic)
      getImageColor(music.al.picUrl, function (color) {
        playerConfig.playerThemeColor = color
        window.storage.set('playerConfig', playerConfig)
        commit('setThemeColor', color)
      })
      commit('playMusic', music)
      commit('saveMusicDetails', musicDetails)
      commit('saveLyricInfo', lyricRes)
      if (autoPlay) {
        commit('startAutoPlay')
        commit('continuePlayMusic')
      }
      // 不需要提交继续播放事件，播放器自动回调  commit('continuePlayMusic')

    },
    /** 保存歌曲详情信息 */
    saveMusicDetails({ commit }, musicDetails) {
      commit('saveMusicDetails', musicDetails)
    },
    /** 暂停音乐 */
    stopMusic({ commit }) {
      commit('stopMusic')
    },
    /** 继续播放音乐 */
    continuePlayMusic({ commit }) {
      commit('continuePlayMusic')
    },
    /** 设置歌词是否显示 */
    setLyricShow({ commit }, lyricShow) {
      commit('setLyricShow', lyricShow)
      playerConfig.lyricShow = lyricShow
      window.storage.set('playerConfig', playerConfig)
    },
    openMusicDrawerShow({ commit }) {
      commit('openMusicDrawerShow')
    },
    closeMusicDrawerShow({ commit }) {
      commit('closeMusicDrawerShow')
    },
    openCompleteMusicDrawerShow({ commit }) {
      commit('openCompleteMusicDrawerShow')
    },
    closeCompleteMusicDrawerShow({ commit }) {
      commit('closeCompleteMusicDrawerShow')
    }
  }
}

export default store
